<?php
class Item_model extends MY_Model
{
    private $table = 'yy_item';
    public function __construct()
    {
        parent::__construct();
    }
    public function get_item_by_id($id)
    {
        $this->db->where("itemid",$id);
        $query=$this->db->get("yy_item");
        return $query->row_array();
    }
    public function get_cat_by_parent_id($parent_id)
    {
        $this->db->where("parent_id",$parent_id);
        $query=$this->db->get($this->table);
        return $query->result_array();
    }
    public function get_item_list()
    {

		$sql = "SELECT * FROM yy_item WHERE isDel=0 ORDER BY onlinestatus desc, addtime DESC";
		$query = $this->db->query($sql);
        return $query->result_array();        
    }
    public function add_item($data)
    {
        $res= $this->db->insert($this->table,$data);
        if($res){
            return $this->db->insert_id();
        }
        return FALSE;
    }
    public function edit_item($id,$data)
    {
        $this->db->where('itemid',$id);
        return $this->db->update($this->table,$data);
    }        
    //删除分类
    public function del_item($itemid)
    {
		$this->db->where('itemid',$itemid);
        return $this->db->update($this->table,array("isDel"=>1));
    }
    /*
     * 获取无限分类ID下面的子类ID集
     *
    */
    public function get_Children_Ids ($cat_id)
    {    
        $ids = "";
		$sql = "SELECT * FROM yy_item_category WHERE parent_id='{$cat_id}'";
		$query = $this->db->query($sql);
        $rs = $query->result_array ();
        if ($rs){ 
            foreach ($rs as $k=>$row){
                $ids .= ','.$row['cat_id'];
                $ids .= $this->get_Children_Ids ($row['cat_id']);
            }            
        }        
        return $ids;
    }   
    
    /**
     * 获取获取当前分类及所有父分类名字
     *@author KevinBai
     */
    public function get_level_catname_bycid($parent_id){
       $sql = "SELECT * FROM yy_item_category WHERE cat_id='{$parent_id}'";
       $query = $this->db->query($sql);
       $tem = $query->row_array();
       $pre_name = "";
       if($query->num_rows()){
           $pre_name.= $this->get_level_catname_bycid($tem['parent_id']); 
           $pre_name.= $tem['cat_name'].'->';   
        }
        return $pre_name;
    }  

                  //插入图书图片
    public function add_item_img($data = array()){
        $res = $this->db->insert("yy_item_attach",$data);
        return $res;
    }     
    //根据id修改图片
    public function edit_item_img($id,$data){
        $this->db->where("id",$id);
        $res = $this->db->update("yy_item_attach",$data);
        return $res;
    }

    public function editStatus($data = array()){

        $this->db->where("itemid",$data['itemid']);
        if($data['type'] == "onlinestatus"){
            $data = array(
                'onlinestatus'=>$data['onlinestatus']
            );
        }
        if($data['type'] == "isDel"){
            $data = array(
                'isDel'=>1
            );
        }

        $res = $this->db->update("yy_item",$data);
        return $res;
    }

     //获取图片列表
    public function get_img_list_byid($id){
        $this->db->where("itemid",$id);
        $query = $this->db->get("yy_item_attach");
        return $query->result_array();
    }
    //根据id删除对应图片
    public function del_attach($id){
        $this->db->where("id",$id);
        $res = $this->db->delete("yy_item_attach");
        return $res;
    }
}

/* End of file flybird/orders_model.php */
/* Location: ./application/models/orders_model.php */